$(function () {
    $("#uploadFrom").submit(upload);
    $("#updatePwd").submit(check_data);
    $("input").focus(clear_error);
});
function upload() {
    $.ajax({
        url: CONTEXT_PATH +"/user/upload",
        method: "post",
        processData: false,
        contentType: false,
        data: new FormData($("#uploadFrom")[0]),
        success: function (data) {
            data=$.parseJSON(data);
            alert(data.msg);
        }
    });
    return false;
}

function check_data() {
    var pwd1 = $("#password").val();
    var pwd2 = $("#confirm-password").val();
    if(pwd1 != pwd2) {
        $("#confirm-password").addClass("is-invalid");
        var content="两次输入的密码不一致！";
        $("#hint").text(content);
        return false;
    }
    if (pwd1.length<8){
        $("#confirm-password").addClass("is-invalid");
        var content="密码长度不能小于8位";
        $("#hint").text(content);
        return false;
    }
    $.post(
        CONTEXT_PATH+"/user/updatePwd",
        {"password":$("input[name='password']").val(),"newPassword":pwd1},
        function (data) {
            data=$.parseJSON(data);
            alert(data.msg);
            if (data.code==0){
                $("#password").val("");
                $("#confirm-password").val("");
                $("input[name='password']").val("");
            }
        }
    );
    return false;
}

function clear_error() {
    $(this).removeClass("is-invalid");
}